PyVistaで Panel を使う¶
PyVistaでは, panel モジュールを vtk.js jupyterlabプロットバックエンドとして使用できます.このバックエンドは,スタンドアロンのVTKビューアまたは緊密に統合された pyvista プロットバックエンドとして使用できます.たとえば,Jupyterノートブック環境で jupyter_backend='panel' を plot または Plotter.show に渡すと,Juptyerと panel を使用したプロットが自動的に有効になります.
例えば,以下は PyVista のロゴです.
from pyvista import demos
demos.plot_logo(background='white', jupyter_backend='panel')
これは, vtk.js への変換でいくつかの詳細が失われるので,完璧なレプリカではないことに注意してください.しかし,ほとんどの場合,これは,Jupyterlab内の pyvista プロットを正確にレンダリングするために使用できます.
例と使用方法¶
Jupyterノートブックで panel を使用する方法は2つあります.これは, mesh.plot() で jupyter_backend を設定することにより,プロット単位で実行できます.
import pyvista as pv
from pyvista import examples
# create a point cloud from lidar data and add height scalars
dataset = examples.download_lidar()
point_cloud = pv.PolyData(dataset.points[::100])
point_cloud['height'] = point_cloud.points[:, 2]
point_cloud.plot(window_size=[500, 500],
jupyter_backend='panel',
cmap='jet',
point_size=2,
background='w')
または,バックエンドをグローバルに設定できます.
import math
import numpy
import numpy as np
import pyvista
from pyvista import examples
# set the global jupyterlab backend. All plots from this point
# onward will use the ``panel`` backend and do not have to be
# specified in ``show``
pyvista.set_jupyter_backend('panel')
# create a sphere for Mars
sphere = pyvista.Sphere(radius=1, theta_resolution=90, phi_resolution=90,
start_theta=270.001, end_theta=270)
sphere.t_coords = numpy.zeros((sphere.points.shape[0], 2))
sphere.t_coords[:, 0] = 0.5 + np.arctan2(-sphere.points[:, 0], sphere.points[:, 1])/(2 * math.pi)
sphere.t_coords[:, 1] = 0.5 + np.arcsin(sphere.points[:, 2]) / math.pi
tex = pyvista.read_texture(examples.download_mars_jpg())
# with a black background
pl = pyvista.Plotter(window_size=[500, 500])
pl.background_color = 'black'
pl.add_mesh(sphere, texture=tex, smooth_shading=False)
pl.show()
構成に関する考慮事項¶
ヘッドレス環境(例: Google Colab,独自のVM)で実行している場合, Xvfb を使用して仮想フレームバッファを必ず起動してください.次のようにbashを使用して起動することもできます.
export DISPLAY=:99.0
export PYVISTA_OFF_SCREEN=true
export PYVISTA_USE_IPYVTK=true
which Xvfb
Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
sleep 3
set +x
exec "$@"
または,組み込みの pyvista.start_xvfb() を使用して起動することもできます. xvfb と libgl1-mesa-glx は必ず次のようにインストールしてください.
sudo apt-get install libgl1-mesa-dev xvfb
または,ご使用の環境で使用されているパッケージマネージャを使用します.